//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package rent.hj.videoplayer.subTitle;



import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;

import rent.hj.videoplayer.SubContentUtil;

public class SmiDecoder extends SubTitleDecoder {
    public SmiDecoder() {
    }

    private List<String> getLangStr(String str) throws IOException {
        StringReader reader = new StringReader(str);
        BufferedReader r = new BufferedReader(reader);
        ArrayList langList = new ArrayList();

        String line;
        while((line = r.readLine()) != null) {
            line = line.trim();
            if(line.startsWith(".")) {
                if(line.indexOf("{") == -1) {
                    langList.add(line.substring(1).trim());
                } else {
                    langList.add(line.substring(1, line.indexOf("{")).trim());
                }
            }
        }

        return langList;
    }

    private String getClassName(String str) {
        StringTokenizer st = new StringTokenizer(str, " ");
        String tmp = null;

        while(st.hasMoreTokens()) {
            tmp = st.nextToken();
            if(tmp.toUpperCase().trim().startsWith("CLASS")) {
                return tmp.trim().split("=")[1].split(" ")[0];
            }
        }

        return null;
    }

    private void getTimeStr(SubTitleContent content, String str) {
        StringTokenizer st = new StringTokenizer(str, " ");
        String tmp = null;

        while(st.hasMoreTokens()) {
            tmp = st.nextToken();
            if(tmp.toUpperCase().trim().startsWith("START")) {
                content.setmSubTitleStartTime(Integer.valueOf(tmp.trim().split("=")[1].split(" ")[0]).intValue());
            } else if(tmp.toUpperCase().trim().startsWith("END")) {
                content.setmSubTitleEndTime(Integer.valueOf(tmp.trim().split("=")[1].split(" ")[0]).intValue());
            }
        }

    }

    private int getStartTime(String str) {
        StringTokenizer st = new StringTokenizer(str, " ");
        String tmp = null;

        while(st.hasMoreTokens()) {
            tmp = st.nextToken();
            if(tmp.toUpperCase().trim().startsWith("START")) {
                return Integer.valueOf(tmp.trim().split("=")[1].split(" ")[0]).intValue();
            }
        }

        return 0;
    }

    public Map<String, List<SubTitleContent>> decodeSubTitle(String subTitlePath, String encode) throws Exception {
        BufferedReader br = SubContentUtil.getEncodeType(subTitlePath);
        TreeMap subTitleMap = new TreeMap();
        new ArrayList();
        SubTitleContent content = null;
        int indexCount = 0;
        int preTagType = SmiTokenizer.SMI_UNKNOWN;
        SmiTokenizer smiTokenizer = new SmiTokenizer(br);
        int tagType = SmiTokenizer.SMI_UNKNOWN;
        List subTitleList = null;
        boolean hasMetNbsp = false;

        for(String subTitleLine = null; (tagType = smiTokenizer.nextHtml()) != SmiTokenizer.SMI_EOF && !SubContentUtil.stopDecodeFlag; preTagType = tagType) {
            if(tagType == SmiTokenizer.SMI_SYNC) {
                if(content != null && content.getmLanguageClass() != null && subTitleMap.get(content.getmLanguageClass()) != null) {
                    if(!hasMetNbsp && content.getmSubTitleEndTime() == 0) {
                        content.setmSubTitleEndTime(this.getStartTime(smiTokenizer.sval));
                    }

                    if(!((List)subTitleMap.get(content.getmLanguageClass())).contains(content)) {
                        ((List)subTitleMap.get(content.getmLanguageClass())).add(content);
                    }
                }

                content = new SubTitleContent();
                hasMetNbsp = false;
                this.getTimeStr(content, smiTokenizer.sval);
                content.setmSubTitleIndex(indexCount++);
            } else if(tagType == SmiTokenizer.SMI_P) {
                if(content != null) {
                    content.setmLanguageClass(this.getClassName(smiTokenizer.sval));
                }
            } else if(tagType == SmiTokenizer.SMI_CSS) {
                List langList = this.getLangStr(smiTokenizer.sval);

                for(int e = 0; e < langList.size(); ++e) {
                    subTitleMap.put((String)langList.get(e), new ArrayList());
                }
            } else if(tagType == SmiTokenizer.SMI_TEXT && content != null) {
                if(subTitleLine == null) {
                    subTitleLine = SubContentUtil.removeHtmlTag(smiTokenizer.sval.toLowerCase().replaceAll("[\\r]?[\\n]", "\n").trim());
                } else {
                    subTitleLine = subTitleLine + SubContentUtil.removeHtmlTag(smiTokenizer.sval.toLowerCase().replaceAll("[\\r]?[\\n]", "\n").trim());
                }

                if("".equals(subTitleLine.trim())) {
                    hasMetNbsp = true;
                    if(content.getmLanguageClass() != null) {
                        subTitleList = (List)subTitleMap.get(content.getmLanguageClass());
                    }

                    if(subTitleList != null && subTitleList.size() != 0) {
                        ((SubTitleContent)subTitleList.get(subTitleList.size() - 1)).setmSubTitleEndTime(content.getmSubTitleStartTime());
                    }

                    content = null;
                } else {
                    if(preTagType == SmiTokenizer.SMI_BR) {
                        content.setmSubTitleLine(content.getmSubTitleLine() + subTitleLine);
                    } else {
                        content.setmSubTitleLine(subTitleLine);
                    }

                    subTitleLine = null;
                }
            } else if(tagType == SmiTokenizer.SMI_BR && content != null) {
                content.setmSubTitleLine(content.getmSubTitleLine() + "<br>");
            } else if(tagType == SmiTokenizer.SMI_BODY && content != null && content != null && content.getmLanguageClass() != null && subTitleMap.get(content.getmLanguageClass()) != null) {
                if(!hasMetNbsp && content.getmSubTitleEndTime() == 0) {
                    content.setmSubTitleEndTime(2147483647);
                }

                if(!((List)subTitleMap.get(content.getmLanguageClass())).contains(content)) {
                    ((List)subTitleMap.get(content.getmLanguageClass())).add(content);
                }
            }
        }

        try {
            br.close();
        } catch (Exception var15) {
            var15.printStackTrace();
        }

        SubContentUtil.SortAllList(subTitleMap);
        return subTitleMap;
    }
}
